package com.orangelife.mobile.widget;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.app.application.CurryApplication;
import com.orangelife.mobile.goodsrent.adapter.GoodsRentDayAdapter;
import com.orangelife.mobile.goodsrent.biz.GoodsInfoCallBack;
import com.orangelife.mobile.goodsrent.dao.GoodsDao;
import com.orangelife.mobile.goodsrent.entity.GoodsInfo;

import java.util.List;
/**
 * Created by Administrator on 2015/12/16.
 */
@Deprecated
public class GoodsRentPopupWindow extends PopupWindow{
    private ListView mListView;
    private List<GoodsInfo> mGoodsInfoList;
    private GoodsRentDayAdapter mGoodsRentDayAdapter;
    private RelativeLayout mRlClearList;
    private GoodsInfoCallBack mGoodsInfoCallBack;
    private View mAnchor;
    private ViewGroup contentView;
    public GoodsRentPopupWindow(List<GoodsInfo> goodsInfoList,GoodsInfoCallBack goosInfoCallBack,View anchor){
        this.mGoodsInfoCallBack = goosInfoCallBack;
        this.mGoodsInfoList = goodsInfoList;
        this.mAnchor = anchor;
        initPopWindow();
        setAdapter();
    }

    private void setAdapter() {
        mGoodsRentDayAdapter = new GoodsRentDayAdapter(mGoodsInfoList,mGoodsInfoCallBack);
        mListView.setAdapter(mGoodsRentDayAdapter);
    }

    private void initPopWindow() {
        LayoutInflater mInflater = LayoutInflater.from(CurryApplication.getInstance().getContext());
        contentView = (ViewGroup)mInflater.inflate(R.layout.popwin_rent_list, null);
        mListView = (ListView)contentView.findViewById(R.id.yfListRecyclerView);
        mRlClearList = (RelativeLayout)contentView.findViewById(R.id.rlClearList);
        mRlClearList.setOnClickListener(new MyOnClicker());
        this.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        this.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        //去掉默认的背景
        this.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        this.setContentView(contentView);
        //点击空白处的时候PopupWindow会消失
        this.setTouchable(true);
        this.setOutsideTouchable(true);
        //如果focusable为false，在一个Activity弹出一个PopupWindow，按返回键，由于PopupWindow没有焦点，会直接退出Activity。如果focusable为true，PopupWindow弹出后，所有的触屏和物理按键都有PopupWindows处理。
        this.setFocusable(true);
        this.showAtLocation((View) mAnchor.getParent(), Gravity.BOTTOM, 0, mAnchor.getHeight());
    }
//    int viewHeight = contentView.getMeasuredHeight();
//    System.out.println("pop高度="+viewHeight);
    class MyOnClicker implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            /**删除表*/
            GoodsDao.getInstance().deleteAllGoodsInfo();
            /**通知页面的购物车数量提示改变*/
            mGoodsInfoCallBack.getGoodsRentNum(0);
            /**通知页面的物品总价改变*/
            mGoodsInfoCallBack.getGoodsTotalPrice("0");
            GoodsRentPopupWindow.this.dismiss();
        }
    }
}
